<?
class Search extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->helper('cookie');
		$this->load->library('encrypt');
	}

	function index()
	{
		date_default_timezone_set('Asia/Bangkok');
			$from_date = strtotime($this->security->xss_clean($this->input->post('from_date')));
			$search_hotel = $this->security->xss_clean($this->input->post('search_hotel'));
			$to_date = strtotime($this->security->xss_clean($this->input->post('to_date')));
			$keyword = $this->security->xss_clean($this->input->post('keyword'));

	

			if ( $search_hotel == 'Check it')
			{
					$config = $this->config->item('db_config');
					$this->load->model('Dbmodel', '', $config);

					$data['info_keyword'] = array(
							'keyword' => $keyword ,
							'date_start' => $this->input->post('from_date') ,
							'date_end' => $this->input->post('to_date') ,
						);

					$objHotel = $this->Dbmodel->select_like('hotel', array('hotel_city' => $keyword), array('hotel_description' => $keyword), "", 
						"room_type, room_type.hotel_id = hotel.hotel_id", 'rt_quantity', 'room_type.hotel_id');

		//			print_r($objHotel);
	//				die;
	
					$temp_room = 0;
					$rt_unavaliable[0] = 0;

					if( $objHotel )
					{
							for($i=0; $i<count($objHotel); $i++)
							{
									$hotel_id = $objHotel[$i]->hotel_id;
									$objNotAvaliable_Hotel = $this->Dbmodel->select_where2('booking', "booking_date_start < $from_date:booking_date_end > $to_date:hotel_id = $hotel_id");

									if ($objNotAvaliable_Hotel)
									{
											for($j=0; $j<count($objNotAvaliable_Hotel); $j++)
											{
													$temp_room += (int)$objNotAvaliable_Hotel[$j]->booking_amount_room;

													// array room unavaliable
								//					$rt_unavaliable[$objNotAvaliable_Hotel[$j]->booking_id][$hotel_id] = $objNotAvaliable_Hotel[$j]->rt_id;
													$rt_unavaliable[] = $objNotAvaliable_Hotel[$j]->rt_id;
										
											}
							
									}
									if ( (int)$objHotel[$i]->rt_quantity > $temp_room )
									{
					//						echo $objHotel[$i]->hotel_name." is avaliable<BR>";
											$data['hotel_avaliable'][$i] = array(
														'hotel_id' => $objHotel[$i]->hotel_id ,
														'hotel_name' => $objHotel[$i]->hotel_name ,
														'hotel_city' => $objHotel[$i]->hotel_city ,
														'hotel_description' => $objHotel[$i]->hotel_description ,
														'hotel_location' => $objHotel[$i]->hotel_location ,
														'hotel_address' => $objHotel[$i]->hotel_address ,
														'hotel_hint' => $objHotel[$i]->hotel_hint ,
													);

											print_r($rt_unavaliable);
											die;

											$objRoomAvaliable = $this->Dbmodel->select_not_in( 'room_type', 'rt_id', $rt_unavaliable, array('hotel_id' => $objHotel[$i]->hotel_id));

											if ( $objRoomAvaliable )
											{
												for($k=0; $k<count($objRoomAvaliable); $k++)
												{
														$data['room_avaliable'][$k] = array(
																'rt_id' => $objRoomAvaliable[$k]->rt_id ,
																'rt_name' => $objRoomAvaliable[$k]->rt_name ,
																'rt_description' => $objRoomAvaliable[$k]->rt_description ,
																'rt_quantity' => $objRoomAvaliable[$k]->rt_quantity ,
																'rt_price' => $objRoomAvaliable[$k]->rt_price ,
																'hotel_id' => $objRoomAvaliable[$k]->hotel_id ,
															);
												}
											}
											$rt_unavaliable[0] = 0;

									}

		//							echo "hotel ".$hotel_id." ".$temp_room."<BR>";
									$temp_room = 0;
									
							}

				//			$objRoomAvaliable = $this->Dbmodel->select_not_in('room_type', 'rt_id', $rt_unavaliable );
							print_r($data['room_avaliable']);
							die;
							
							
							$this->load->view('search_hotelview', $data);
					}

	//				print_r($temp_rom);
	//				die;

	//				$objAvaliable_Hotel = $this->Dbmodel->select_where2('booking', "booking_date_start < $from_date:booking_date_end > $to_date");

				

			}

	}  // end function index()

	function room($hotel_id)
	{
		
	}

}

?>